var Behaviour = {list:new Array, register:function (sheet) {
	Behaviour.list.push(sheet);
}, start:function () {
	if (window.addEventListener) {
		window.addEventListener("load", function () {
			Behaviour.apply();
		}, false);
	} else {
		if (window.attachEvent) {
			window.attachEvent("onload", function () {
				Behaviour.apply();
			});
		}
	}
}, apply:function () {
	for (h = 0; sheet = Behaviour.list[h]; ++h) {
		Behaviour.applyRules(sheet);
	}
}, applyRules:function (sheet) {
	for (selector in sheet) {
		list = document.getElementsBySelector(selector);
		if (!list) {
			continue;
		}
		for (i = 0; element = list[i]; ++i) {
			sheet[selector](element);
		}
	}
}, getSelectBox:function (elt) {
	if (elt.parentElement) {
		return elt.parentElement.parentElement.parentElement.parentElement.parentElement;
	} else {
		if (elt.parentNode) {
			return elt.parentNode.parentNode.parentNode.parentNode.parentNode;
		}
	}
},addLog:function(logstr){
	try {
		if (lex.IS_IE && typeof(document.applets[0])!='undefined') {
			document.applets[0].addLog(logstr);
		}
	} catch(e) {}
},toggleLog:function(b){
	try {
		if (lex.IS_IE && typeof(document.applets[0])!='undefined') {
			if (b)
				document.applets[0].setShow(true);
			else
				document.applets[0].setShow(false);
		}
	} catch(e) {}
},putApplet:function(){
	if(lex.IS_IE && document.applets.length == 0 && navigator.javaEnabled()) {
		document.writeln("<applet codebase=\".\"");
		document.writeln("archive=\"/logApplet.jar\"");
		document.writeln("code=\"com.ali.lex.applet.LogApplet.class\"");
		document.writeln("name=\"LogApplet\"");
		document.writeln("width=\"1\"");
		document.writeln("height=\"1\">");
		document.writeln("</applet>");
	}
}};
Behaviour.start();
//Behaviour.putApplet();

if (lex.IS_IE)
{
	var OPEN_LOG = false;
	function getKeyCode() {
		var code = event.keyCode;
		if (code == 93) {//window context menu key
			OPEN_LOG = !OPEN_LOG;
			if (OPEN_LOG)
				Behaviour.toggleLog(true);
			else
				Behaviour.toggleLog(false);
		}
	}
	document.onkeydown = getKeyCode;
}

var __sto = setTimeout;
window.setTimeoutEnhance = function(callback,timeout,param)
{
    var args = Array.prototype.slice.call(arguments,2);
    var _cb = function()
    {
        callback.apply(null,args);
    }
    __sto(_cb,timeout);
}

var checkIntegrity_times = 10;
var checkIntegrity_cur_count = 0;
var checkIntegrity_notfound_list = [];
function checkIntegrity(ObjectNameArray,ck) {
	Behaviour.addLog("in checkIntegrity");
	checkIntegrity_cur_count ++;
	Behaviour.addLog("in checkIntegrity:"+checkIntegrity_cur_count);
	if (checkIntegrity_cur_count <= checkIntegrity_times)
	{
		for (var i=0;i<ObjectNameArray.length;i++) {
			objName = ObjectNameArray[i];
			Behaviour.addLog("in checkIntegrity:finding " + objName);
			if (eval("typeof("+objName+")") == "undefined")
			{
				Behaviour.addLog("in checkIntegrity:"+objName+" is not found");
				if(!checkIntegrity_in_notfound_list(checkIntegrity_notfound_list,objName))
					checkIntegrity_notfound_list.push(objName);
				Behaviour.addLog("in checkIntegrity:timer start.....");
				window.setTimeoutEnhance(checkIntegrity,200,ObjectNameArray,ck);
				return;
			}
		}
	}else{
		alert("checkIntegrity timeout: not found [" + checkIntegrity_notfound_list + "]");
		return;
	}
	if (ck){
		ck();
	}else{
		Behaviour.addLog("in checkIntegrity:timer start.....");
		window.setTimeoutEnhance(checkIntegrity,200,ObjectNameArray,ck);
		return;
	}
}

function checkIntegrity_in_notfound_list(checkIntegrity_notfound_list,objName){
	for (var i=0;i<checkIntegrity_notfound_list.length;i++)
	{
		if (checkIntegrity_notfound_list[i] == objName)
		{
			return true;
		}
	}
	return false;
}

function getAllChildren(e) {
	return e.all ? e.all : e.getElementsByTagName("*");
}
document.getElementsBySelector = function (selector) {
	if (!document.getElementsByTagName) {
		return new Array();
	}
	var tokens = selector.split(" ");
	var currentContext = new Array(document);
	for (var i = 0; i < tokens.length; i++) {
		token = tokens[i].replace(/^\s+/, "").replace(/\s+$/, "");
		if (token.indexOf("#") > -1) {
			var bits = token.split("#");
			var tagName = bits[0];
			var id = bits[1];
			var element = document.getElementById(id);
			if (element && tagName && element.nodeName.toLowerCase() != tagName) {
				return new Array();
			}
			currentContext = new Array(element);
			continue;
		}
		if (token.indexOf(".") > -1) {
			var bits = token.split(".");
			var tagName = bits[0];
			var className = bits[1];
			if (!tagName) {
				tagName = "*";
			}
			var found = new Array;
			var foundCount = 0;
			for (var h = 0; h < currentContext.length; h++) {
				var elements;
				if (tagName == "*") {
					elements = getAllChildren(currentContext[h]);
				} else {
					elements = currentContext[h].getElementsByTagName(tagName);
				}
				for (var j = 0; j < elements.length; j++) {
					found[foundCount++] = elements[j];
				}
			}
			currentContext = new Array;
			var currentContextIndex = 0;
			for (var k = 0; k < found.length; k++) {
				if (found[k].className && found[k].className.match(new RegExp("\\b" + className + "\\b"))) {
					currentContext[currentContextIndex++] = found[k];
				}
			}
			continue;
		}
		if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
			var tagName = RegExp.$1;
			var attrName = RegExp.$2;
			var attrOperator = RegExp.$3;
			var attrValue = RegExp.$4;
			if (!tagName) {
				tagName = "*";
			}
			var found = new Array;
			var foundCount = 0;
			for (var h = 0; h < currentContext.length; h++) {
				var elements;
				if (tagName == "*") {
					elements = getAllChildren(currentContext[h]);
				} else {
					elements = currentContext[h].getElementsByTagName(tagName);
				}
				for (var j = 0; j < elements.length; j++) {
					found[foundCount++] = elements[j];
				}
			}
			currentContext = new Array;
			var currentContextIndex = 0;
			var checkFunction; // This function will be used to filter the elements
			switch (attrOperator) {
			  case "=": // Equality
				checkFunction = function (e) {
					return (e.getAttribute(attrName) == attrValue);
				};
				break;
			  case "~": // Match one of space seperated words
				checkFunction = function (e) {
					return (e.getAttribute(attrName).match(new RegExp("\\b" + attrValue + "\\b")));
				};
				break;
			  case "|": // Match start with value followed by optional hyphen
				checkFunction = function (e) {
					return (e.getAttribute(attrName).match(new RegExp("^" + attrValue + "-?")));
				};
				break;
			  case "^": // Match starts with value
				checkFunction = function (e) {
					return (e.getAttribute(attrName).indexOf(attrValue) == 0);
				};
				break;
			  case "$": // Match ends with value - fails with "Warning" in Opera 7
				checkFunction = function (e) {
					return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length);
				};
				break;
			  case "*": // Match ends with value
				checkFunction = function (e) {
					return (e.getAttribute(attrName).indexOf(attrValue) > -1);
				};
				break;
			  default: // Just test for existence of attribute
				checkFunction = function (e) {
					return e.getAttribute(attrName);
				};
			}
			currentContext = new Array;
			var currentContextIndex = 0;
			for (var k = 0; k < found.length; k++) {
				if (checkFunction(found[k])) {
					currentContext[currentContextIndex++] = found[k];
				}
			}
			continue;
		}
		if (!currentContext[0]) {
			return;
		}
		tagName = token;
		var found = new Array;
		var foundCount = 0;
		for (var h = 0; h < currentContext.length; h++) {
			var elements = currentContext[h].getElementsByTagName(tagName);
			for (var j = 0; j < elements.length; j++) {
				found[foundCount++] = elements[j];
			}
		}
		currentContext = found;
	}
	return currentContext;
};

